package cn.huolabao.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
@Slf4j
public class AuthExceptionHandler {

    @ExceptionHandler(InvalidGrantException.class)
    public ResponseEntity<Map<String, String>> handleInvalidGrantException(InvalidGrantException e) {
        e.printStackTrace();
        Map<String, String> map = new HashMap<>();
        map.put("msg", e.getMessage());
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    @ExceptionHandler(BadCredentialsException.class)
    public ResponseEntity<Map<String, String>> handleBadCredentialsException(BadCredentialsException e) {
        e.printStackTrace();
        Map<String, String> map = new HashMap<>();
        map.put("msg", e.getMessage());
        return new ResponseEntity<>(map, HttpStatus.OK);
    }


    @ExceptionHandler({InternalAuthenticationServiceException.class})
    public ResponseEntity<Map<String, String>> handleInternalAuthenticationServiceException(InternalAuthenticationServiceException e) {
        e.printStackTrace();
        Map<String, String> map = new HashMap<>();
        map.put("msg", e.getMessage());
        return new ResponseEntity<>(map, HttpStatus.OK);
    }
}
